Shader "Terrain/ClipMap"
{
	Properties
	{
		_HeightMap("HeightMap", 2D) = "white" {}
		_Viewer("Viewer", Vector) = (0, 0, 0, 0)
		_WorldPosAndScale("WorldPosAndScale", Vector) = (0, 0, 0, 0)
		_TexturePosAndScale("TexturePosAndScale", Vector) = (0, 0, 0, 0)
	}
	SubShader
	{
		Pass
		{
			CGPROGRAM

			#pragma vertex vert
			#pragma fragment frag
			#pragma target 5.0

			#include "UnityCG.cginc"

			sampler2D _HeightMap;
			float4 _Viewer;
			float4 _WorldPosAndScale;
			float4 _TexturePosAndScale;
			
			struct vs_input
			{
				float4 gridPos : POSITION;
			};

			struct ps_input
			{
				float4 pos : SV_POSITION;
				float2 uv : TEXCOORD0;
			};

			ps_input vert(vs_input v, uint id : SV_VertexID)
			{
				float2 world_pos = v.gridPos.xz * _WorldPosAndScale.zw + _WorldPosAndScale.xy;
				float2 texcoord = v.gridPos.xz * _TexturePosAndScale.zw + _TexturePosAndScale.xy;
				
				float height = tex2Dlod(_HeightMap, float4(texcoord, 0, 0)).r;
				float4 vert = float4(world_pos.x, height*10, world_pos.y, 1);
				
				ps_input o;
				o.pos = mul(UNITY_MATRIX_VP, vert);
				o.uv = texcoord;
				return o;
			}

			float4 frag(ps_input i) : COLOR
			{
				float height = tex2Dlod(_HeightMap, float4(i.uv, 0, 0)).r;
				return float4(height, height, height, 1);
			}

			ENDCG
		}
	}
	Fallback Off
}